import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2024-12-16
 * Time: 21:09
 */
public class test2 {
    /*
    NewCoder BM47
    寻找第K大
    有一个整数数组，请你根据快速排序的思路，找出数组中第 k 大的数。
     */

    public int findKth (int[] nums, int n, int k) {
        // write code here
        qsort(nums,0,n-1);
        return nums[n-k];
    }

    public void qsort(int[] nums,int l,int r){
        if(l>=r){
            return;
        }
        int key = nums[new Random().nextInt(r-l+1)+l];
        int left = l-1;
        int right = r+1;
        int i = l;
        while(i<right){
            if(nums[i]<key){
                swap(nums,++left,i++);
            }else if(nums[i]==key){
                i++;
            }else{
                swap(nums,--right,i);
            }
        }

        qsort(nums,l,left);
        qsort(nums,right,r);
    }

    public void swap(int[] nums,int left,int right){
        int tmp = nums[left];
        nums[left] = nums[right];
        nums[right] = tmp;
    }
}
